<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>  
<html>  
    <head>  
    <meta charset="utf-8">  
    <title>数据源配置</title>
	 	<script type="text/javascript">
			//var groupId = '${groupId}';
			var pageNumber;
			var pageSize;
			var c = document.documentElement.clientHeight;
			
			//数据源类型
			var sourceTypes = new Array();
			
			function sourceTypeFormatter(value){
				for(var i=0; i<sourceTypes.length; i++){
					if (sourceTypes[i].code == value) return sourceTypes[i].typeCname;
				}
				return value;
			}
			
			function loadSourceInfo(){
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/loadSourceInfo',
					type:'post',
					data:{},
					success:function(data){
						for(var i = 0; i < data[1].length; i++){
							var sourceType = {};
							sourceType.code = data[1][i].code;
							//alert(data[1][i].code);
							sourceType.typeCname = data[1][i].typeCname;
							sourceTypes[i] = sourceType;
						}
						loadData();
					}
				});
			}
			
			$(function(){
				pageNumber = 1;
				pageSize = 15;
				c = document.documentElement.clientHeight-35;
				
				loadSourceInfo();
			});
			
			
			var lastIndex;
			function loadData(){
				lastIndex = -1;
				var groupTableId = $('input[name=groupTableId]').val();
				var addRow = -1;
				$('#maintainClnTable').datagrid({  
				    url:'${pageContext.request.contextPath}/edi/queryExcClnByIds?groupId='+groupId+'&groupTableId='+groupTableId,  
				    width:function(){return document.documentElement.clientWidth-45},
					height:c,
					pageList:[20,30,50,100],
				    iconCls:'icon-save',
					nowrap: false,
					striped: true,				 
					collapsible:true,
					autoRowHeight: false,		
					sortName: 'colName',
					sortOrder: 'desc',
					remoteSort: false,
					idField:'code',
					fitColumns:true,
					singleSelect:true,
				    columns:[[  
				        {field:'colId',title:'列编号',width:100},  
				        {field:'colName',title:'列名',width:100,editor:'text'},  
				        {field:'colCname',title:'列中文名',width:100,align:'right',
				        	editor:'text'},  
				        {field:'editStyle',title:'数据源类型',width:100,align:'right',
				        	formatter:sourceTypeFormatter,
				        	editor:{type:'combobox',options:{valueField:'code',textField:'typeCname',data:sourceTypes}}},  
				        {field:'sortNum',title:'排序号',width:100,align:'right',editor:'text'},  
				        {field:'memo',title:'备注',width:100,align:'right',editor:'text'}  
				    ]],
					rownumbers:true,
					toolbar:[{
						id:'btnadd',
						text:'从Ds中获取字段',
						iconCls:'icon-add',
						handler:function(){
							loadDataFromDS();
						}					
					},'-',{
						id:'btnsql',
						text:'从SQL中获取字段',
						iconCls:'icon-add',
						handler:function(){
							loadDataFromSQL();
						}					
					},'-',{
						id:'btnsave',
						text:'保存',
						iconCls:'icon-save',
						handler:function(){
							$('#maintainClnTable').datagrid('endEdit', lastIndex);
							var rowsT = $("#maintainClnTable").datagrid("getRows"); 
							var columns = new Array();
							for(var i = 0; i < rowsT.length; i++){
								columns[i] = {
								'colId':rowsT[i].colId,
								'groupId':groupId,
								'groupTableId':$('input[name=groupTableId]').val(),
								'colName':rowsT[i].colName,
								'colCname':rowsT[i].colCname,
								'editStyle':rowsT[i].editStyle,
								'sortNum':rowsT[i].sortNum,
								'memo':rowsT[i].memo};
							}
							var excColumns = {};
							excColumns = columns;
							$.ajax({
				 				url:'${pageContext.request.contextPath}/edi/saveClns',
				 				type:'post',
                  				contentType: "application/json;charset=utf-8",
				 				data:JSON.stringify(excColumns),
				 				dataType: "json",
				 				success:function(data){
				 					if(data.msg == 'success'){
				 						$.messager.alert('提示框','保存成功','info');
				 					}else{
				 						$.messager.alert('错误提示框',data.msg,'error');
				 					}
				 				}
				 			});
						}	
					},'-',{
						id:'btnreload',
						text:'刷新',
						iconCls:'icon-reload',
						handler:function(){
							$.ajax({
				 				url:'${pageContext.request.contextPath}/edi/queryExcClnByIds',
				 				data:{groupTableId:$('input[name=groupTableId]').val(),groupId:groupId},
				 				success:function(data){
							 		$('#maintainClnTable').datagrid('loadData',data);
				 				}
				 			});
						}					
					}],
					onBeforeLoad:function(){
						$('#maintainClnTable').datagrid('rejectChanges');
					},
					onDblClickRow:function(rowIndex){
						if (lastIndex != rowIndex){
							$('#maintainClnTable').datagrid('endEdit', lastIndex);
							$('#maintainClnTable').datagrid('beginEdit', rowIndex);
						}
						lastIndex = rowIndex;
					}
				});
			}
			function loadDataFromDS(){
				$.ajax({
	 				url:'${pageContext.request.contextPath}/edi/queryExcClnFromDS',
	 				data:{groupTableId:$('input[name=groupTableId]').val(),groupId:groupId},
	 				success:function(data){
			 			if(data.msg=='success'){
				 			lastIndex = -1;
				 			$('#maintainClnTable').datagrid('loadData',data);
			 			}else if(data.msg=='info'){
			 				$.messager.alert('提示框',data.extendinfo,'info');
			 			}else{
			 				$.messager.alert('错误提示框',data.msg,'error');
			 			}
	 				}
	 			});
			}	
			
			function loadDataFromSQL(){
				
				$.ajax({
	 				url:'${pageContext.request.contextPath}/edi/queryExcClnFromSQL',
	 				data:{groupTableId:$('input[name=groupTableId]').val(),groupId:groupId},
	 				success:function(data){
			 			if(data.msg=='success'){
				 			lastIndex = -1;
				 			$('#maintainClnTable').datagrid('loadData',data);
			 			}else if(data.msg=='info'){
			 				$.messager.alert('提示框',data.extendinfo,'info');
			 			}else{
			 				$.messager.alert('错误提示框',data.msg,'error');
			 			}
	 				}
	 			});
			}
	 	</script>
    </head>  
    <body>  
    	<!-- 数据源查询界面 -->
    	<table id="maintainClnTable"></table>  
	</body>  
</html>  